<?php
/** 
*
* @author David Lewis (Highway of Life) http://startrekguide.com/forum/
*
* @package acp
* @version $Id: acp_topic_tags.php 90 2007-07-04 17:08:07Z highwayoflife $
* @copyright (c) 2007 Star Trek Guide Group 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

/**
* acp_topic_tags
* Topic Tags Administration
* @package acp
*/
class acp_topic_tags
{
	var $u_action;

	function main($id, $mode)
	{
		global $config, $user, $phpbb_root_path, $phpEx, $db, $template;
		
		include_once($phpbb_root_path . 'includes/functions_topic_tags.' . $phpEx);
		
		$topic_tags_version = '0.1.0';
		$disable = request_var('mod', '');
		
		if ($disable == 'disable')
		{
			set_config('topic_tags_version', 0);
		}
		else if ($disable == 'enable')
		{
			set_config('topic_tags_version', $topic_tags_version);
		}
		
		if (!isset($config['topic_tags_version']))
		{
			if (confirm_box(true))
			{
				include($phpbb_root_path . 'includes/acp/info/acp_topic_tags.' . $phpEx);
				$mod = new acp_topic_tags_info();
				
				$mod->install($topic_tags_version);
			}
			else 
			{
				$s_hidden_fields = build_hidden_fields(array(
					'mode'		=> $mode,
					'id'		=> $id,
					)
				);
	
				confirm_box(false, $user->lang['CONFIRM_TOPIC_TAGS_INSTALL'], $s_hidden_fields);
			}
		}
		else if (version_compare($config['topic_tags_version'], $topic_tags_version, '<') && $config['topic_tags_version'] != 0)
		{
			include($phpbb_root_path . 'includes/acp/info/acp_topic_tags.' . $phpEx);
			$mod = new acp_topic_tags_info($topic_tags_version);
			
			$mod->update();
		}
		else if ($config['topic_tags_version'] == 0)
		{
			trigger_error(sprintf($user->lang['TOPIC_TAGS_MOD_DISABLED'], append_sid($u_action, 'mod=enable')));
		}
		
		//-----------------------------------------------------------------------//
		
		$global_tag_list = request_var('global_tag', array(''));
		$global_tags = request_var('global_tags', '');
		$submit = (isset($_REQUEST['submit'])) ? true : false;
		$mark	= (isset($_REQUEST['mark'])) ? request_var('mark', array(0)) : array();
		$msg = array();
		
		if (sizeof($mark))
		{
			$sql = 'SELECT topic_tag_id, topic_tag_name 
					FROM ' . TOPIC_TAGS_TABLE . '
					WHERE ' . $db->sql_in_set('topic_tag_id', $mark);
			$result = $db->sql_query($sql);
		
			$tags_name_ary = $tags_affected = array();
			
			while ($row = $db->sql_fetchrow($result))
			{
				$tags_name_ary[$row['topic_tag_id']] = $row['topic_tag_name'];
				$tags_affected[] = $row['topic_tag_id'];
			}
			$db->sql_freeresult($result);
			
			if (sizeof($tags_affected))
			{
				if (confirm_box(true))
				{
					$sql = 'DELETE FROM ' . TOPIC_TAGS_TABLE . ' WHERE ' . $db->sql_in_set('topic_tag_id', $tags_affected);
					$db->sql_query($sql);
					
					$sql = 'UPDATE ' . TOPICS_TABLE . ' SET topic_tag_id = 0 WHERE ' . $db->sql_in_set('topic_tag_id', $tags_affected);
					$db->sql_query($sql);
					
					$msg[] = $user->lang['GLOBAL_TOPIC_TAGS_DELETED'];
					
					add_log('admin', 'LOG_GLOBAL_TOPIC_TAGS_DELETED', strip_tags(html_entity_decode(implode(', ', $tags_name_ary))));
				}
				else
				{
					$s_hidden_fields = build_hidden_fields(array(
						'global_tag'		=> $global_tag_list,
						'global_tags'		=> $global_tags,
						'mark'				=> $mark,
						'id'				=> $id,
						'mode'				=> $mode,
						'submit'			=> $submit,
						'redirect'			=> $this->u_action)
					);
					
					confirm_box(false, $user->lang['CONFIRM_TOPIC_TAGS_DELETE'], $s_hidden_fields);
				}
			}

		}
		
		if (sizeof($global_tag_list) && $submit)
		{
			$sql = 'SELECT topic_tag_id, topic_tag_name
					FROM ' . TOPIC_TAGS_TABLE . '
					WHERE ' . $db->sql_in_set('topic_tag_id', array_keys($global_tag_list));
			$result = $db->sql_query($sql);
			
			$global_tag_ids = array();
			while ($row = $db->sql_fetchrow($result))
			{
				$global_tag_ids[$row['topic_tag_id']] = $row['topic_tag_name'];
			}
			
			$tags_updated = array();
			
			foreach($global_tag_list as $id => $value)
			{
				// only update if there has been a change
				if ($value != $global_tag_ids[$id])
				{
					$sql = 'UPDATE ' . TOPIC_TAGS_TABLE . " SET topic_tag_name = '" . $db->sql_escape($value) . "'
							WHERE topic_tag_id = $id";
					$db->sql_query($sql);
					$tags_updated[] = $value;
				}
			}
			
			if (sizeof($tags_updated))
			{
				$msg[] = $user->lang['GLOBAL_TOPIC_TAGS_UPDATED'];
				
				add_log('admin', 'LOG_GLOBAL_TOPIC_TAGS_EDITED', strip_tags(html_entity_decode(implode(', ', $tags_updated))));
			}
		}
		
		if ($global_tags && $submit)
		{
			$global_tag_ary = explode("\n", $global_tags);
			
			$sql_ary = array();
			foreach ($global_tag_ary as $id => $value)
			{
				$sql_ary[] = array(
					'topic_tag_name'	=> $value,
					'topic_tag_global'	=> 1,
				);
			}
			
			if (sizeof($sql_ary))
			{
				$db->sql_multi_insert(TOPIC_TAGS_TABLE, $sql_ary);
				$msg[] = $user->lang['GLOBAL_TOPIC_TAGS_ADDED'];
				
				add_log('admin', 'LOG_GLOBAL_TOPIC_TAGS_ADDED', strip_tags(html_entity_decode(implode(', ', $global_tag_ary))));
			}
		}
		
		$sql = 'SELECT *
				FROM ' . TOPIC_TAGS_TABLE . ' WHERE topic_tag_global = 1';
		$result = $db->sql_query($sql);
		
		while ($row = $db->sql_fetchrow($result))
		{
			$template->assign_block_vars('globalrow', array(
				'TAG_ID'	=> $row['topic_tag_id'],
				'TAG_NAME_PARSED'	=> html_entity_decode($row['topic_tag_name']),
				'TAG_NAME'	=> $row['topic_tag_name'],
				'TAG_COLOR'	=> $row['topic_tag_colour'],
				)
			);
		}
		$db->sql_freeresult($result);
		
		$template->assign_vars(array(
			'TOPIC_TAGS_MOD_DISABLE'	=> append_sid($this->u_action, 'mod=disable'),
			'S_MESSAGE'					=> (sizeof($msg)) ? implode('<br />', $msg) : false,
			'S_GLOBAL_TOPIC_TAGS'		=> true,
		));
		
		$this->tpl_name = 'acp_topic_tags';
		$this->page_title = 'ACP_GLOBAL_TOPIC_TAGS';
	}
}

?>